package com.ssy.lingxi.settle.accounts.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.component.redis.service.IRedisStringUtils;
import com.ssy.lingxi.settle.accounts.model.constant.SettleAccountsServiceContants;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;

/**
 * 流水号生成工具类
 * @author 伍将
 * @date 2021/3/24
 * @version 2.0.0
 */
public class SerialNumberUtils {

    /**
     * 生成流水号
     * @param redisKey redisKey
     * @param dateFormat 日期格式
     * @param numberLen 数字长度
     */
    public static String getNumber(IRedisStringUtils redisStringUtils, String redisKey, String dateFormat, Integer numberLen) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String date = sdf.format(new Date());
        Long number = redisStringUtils.increment(redisKey, Constants.REDIS_ORDER_INDEX);
        return date + StringUtils.leftPad(String.valueOf(number), numberLen, '0');
    }

    /**
     * 生成对账单流水号
     * @param redisKey redisKey
     */
    public static String getReconciliationNo(IRedisStringUtils redisStringUtils, String redisKey) {
        String s = LocalDateTime.now().format(SettleAccountsServiceContants.DEFAULT_DATE_FORMATTER).replace("-","");
        Long number = redisStringUtils.increment(redisKey, Constants.REDIS_SETTLEMENT_INDEX);
        return SettleAccountsServiceContants.SETTLEMENT_TITLE + s + StringUtils.leftPad(String.valueOf(number), 5, '0');
    }

    public static String getApplyAmountNo(IRedisStringUtils redisStringUtils, String redisKey) {
        String s = LocalDateTime.now().format(SettleAccountsServiceContants.DEFAULT_DATE_FORMATTER).replace("-","");
        Long number = redisStringUtils.increment(redisKey, Constants.REDIS_SETTLEMENT_INDEX);
        return SettleAccountsServiceContants.APPLY_AMOUNT + s + StringUtils.leftPad(String.valueOf(number), 5, '0');
    }
    /**
     * 生成随机（重复概率非常低）的请款单号
     * @return 请款单号
     */
    public static String randomUniqueNumber() {
        return "Q".concat(RandomStringUtils.random(6, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"));
    }

    /**
     * Json序列化对象
     * @param obj 对象
     * @return json字符串
     */
    public static String serializeObject(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new JavaTimeModule());
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * Json反序列化对象
     *
     * @param json   Json字符串
     * @param tClass 反序列化对象的Class
     * @return 对象
     */
    public static <T> T deserializeObject(String json, Class<T> tClass) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.registerModule(new JavaTimeModule());
            return mapper.readValue(json, tClass);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 屏蔽字符串
     *
     * @param plainString 字符串
     * @return 屏蔽字符串
     */
    public static String coverString(String plainString) {
        if (!org.springframework.util.StringUtils.hasText(plainString)) {
            return "";
        }

        final String maskString = "*";
        switch (plainString.length()) {
            case 1:
                return plainString;
            case 2:
                return plainString.charAt(0) + maskString;
            case 3:
                return plainString.charAt(0) + maskString + plainString.substring(2);
            case 4:
                return plainString.charAt(0) + String.join("", Collections.nCopies(2, maskString)) + plainString.substring(2);
            case 5:
            case 6:
                return plainString.substring(0, 2) + String.join("",Collections.nCopies(2, maskString)) + plainString.substring(4);
            default:
                return plainString.substring(0, 3) + String.join("",Collections.nCopies(3, maskString)) + plainString.substring(6);
        }
    }
}
